package com.ruoyi.web.controller.sis;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.entity.SisApprove;
import com.ruoyi.system.entity.SisInventoryCount;
import com.ruoyi.system.service.SisApproveService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (SisApprove)SIS审批表控制层
 *
 * @author makejava
 * @since 2023-08-01 10:17:49
 */
@RestController
@RequestMapping("sisApprove")
public class SisApproveController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private SisApproveService sisApproveService;

    /**
     * 分页查询
     *
     * @param sisApprove 筛选条件
     * @return 查询结果
     */
    @GetMapping
    public TableDataInfo queryByPage(SisApprove sisApprove) {
        startPage();
        List<SisApprove> sisApproves = this.sisApproveService.queryByPage(sisApprove);
        return getDataTable(sisApproves);
    }

    /**
     * 根据创建人查询列表
     *
     * @return
     */
    @GetMapping("/listByCreateUser")
    public TableDataInfo queryByPageCreateUser() {
        SisApprove sisApprove = new SisApprove();
        sisApprove.setCreateUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        startPage();
        List<SisApprove> sisApproves = this.sisApproveService.queryByPage(sisApprove);
        return getDataTable(sisApproves);
    }

    /**
     * 根据审批人查询列表
     *
     * @return
     */
    @GetMapping("/listByPhaseUser")
    public TableDataInfo listByPhaseUser() {
        SisApprove sisApprove = new SisApprove();
        sisApprove.setPhaseUserName(SecurityUtils.getLoginUser().getUser().getNickName());
        startPage();
        List<SisApprove> sisApproves = this.sisApproveService.queryByPage(sisApprove);
        return getDataTable(sisApproves);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult queryById(@PathVariable("id") Integer id) {
        SisApprove sisApprove = this.sisApproveService.queryById(id);
        return AjaxResult.success(sisApprove);
    }

    /**
     * 新增数据
     *
     * @param sisApprove 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<SisApprove> add(@RequestBody SisApprove sisApprove) {
        return ResponseEntity.ok(this.sisApproveService.insert(sisApprove));
    }

    /**
     * 编辑数据
     *
     * @param sisApprove 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<SisApprove> edit(@RequestBody SisApprove sisApprove) {
        return ResponseEntity.ok(this.sisApproveService.update(sisApprove));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.sisApproveService.deleteById(id));
    }


    /**
     * 审批
     *
     * @param sisApprove 实体
     * @return 审批
     */
    @PostMapping("/approve")
    public AjaxResult approve(@RequestBody SisApprove sisApprove) {
        this.sisApproveService.approve(sisApprove);
        return AjaxResult.success();
    }

    @GetMapping("get/inventory/count")
    public AjaxResult getInventoryCount() {
        SisInventoryCount inventoryCount = sisApproveService.getInventoryCount();
        return AjaxResult.success(inventoryCount);
    }

}

